package com.moon;

import java.util.Set;

import cn.yueliangbaba.R;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.RequestParams;
import com.sys.DemoApp;
import com.umeng.message.UmengRegistrar;
import com.util.MgqDataHandler;
import com.util.MgqRestClient;
import com.util.PreferencesUtils;
import com.util.ToastUtil;
import com.util.Utils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

public class ResetPwdActivity extends BaseActivity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_set_pwd);
		
		((TextView) findViewById(R.id.center)).setText("设置密码");
		findViewById(R.id.next).setOnClickListener(this);
		findViewById(R.id.left).setOnClickListener(this);
	}
	
	private void sendRequest() {
		String pwd0 = ((EditText)findViewById(R.id.phone)).getText().toString();
		String pwd1 = ((EditText)findViewById(R.id.code)).getText().toString();
		
		if(pwd0.length() == 0 || !pwd0.equals(pwd1))
		{
			ToastUtil.showToastWithAlertPic("密码不能为空/密码不一致");
			return;
		}
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					sendRequestLogin();
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("telephone", getIntent().getStringExtra("telephone"));
		params.put("password", pwd1);
		
		MgqRestClient.post("update_profile", params, loginHandler);
	}
	
	public void sendRequestMember() {
		MgqDataHandler loginHandler = new MgqDataHandler(this, false, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				JSONObject array = ruser.getJSONObject("members");
				if(array != null && array.size() > 0)
				{
					((DemoApp)getApplicationContext()).mMembers = ruser.getJSONObject("members");
					PreferencesUtils.putString(ResetPwdActivity.this, "all_members", 
							JSON.toJSONString(((DemoApp)getApplicationContext()).mMembers));
				}
				
				startActivity(new Intent(ResetPwdActivity.this,MainTabActivity.class));
				finish();
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("id", PreferencesUtils.getString(this, "userId"));
		MgqRestClient.get("get_my_group_member_list", params, loginHandler);
	}

	private void sendRequestLogin() {
		final String phone = getIntent().getStringExtra("telephone");
		final String pwd1 = ((EditText)findViewById(R.id.code)).getText().toString();
		
		MgqDataHandler loginHandler = new MgqDataHandler(this, true, false) {
			@Override
			public void onSuccess(String response) {
				JSONObject ruser = JSON.parseObject(response);
				if (ruser.getString("statusCode").equals("0")) {
					JSONObject member = ruser.getJSONObject("member");
					
					PreferencesUtils.putString(ResetPwdActivity.this, "username", phone);
					PreferencesUtils.putString(ResetPwdActivity.this, "password", pwd1);
					PreferencesUtils.putString(ResetPwdActivity.this, "userId", member.getString("id"));
					PreferencesUtils.putString(ResetPwdActivity.this, "member", JSON.toJSONString(member));
					PreferencesUtils.putString(ResetPwdActivity.this, "classes", JSON.toJSONString(member.getJSONArray("classes")));
					PreferencesUtils.putString(ResetPwdActivity.this, "children", JSON.toJSONString(member.getJSONArray("children")));

					((DemoApp)getApplicationContext()).setClasses();
					String token = member.getString("im_token");
					if(token != null && token.length() > 0)
					{
						PreferencesUtils.putString(ResetPwdActivity.this, "im_token", token);
						((DemoApp)getApplicationContext()).connect(token);
					}
					
					sendRequestMember();
				}
				else 
				{
					ToastUtil.showToastWithAlertPic(ruser.getString("message"));
				}
			}
			
			@Override
			public void onFailure(Throwable ble) {
				super.onFailure(ble);
				ToastUtil.showToastWithAlertPic("网络状况不佳，请稍后重试...");
			}
		};
		
		RequestParams params = new RequestParams();
		params.put("phonenumber", phone);
		params.put("password", pwd1);
		params.put("android_token", UmengRegistrar.getRegistrationId(this));//ios_token
		MgqRestClient.get("member_login", params, loginHandler);
	}
	
	@Override
	public void onClick(View arg0) {
		if(arg0.getId() == R.id.left) finish();
		else sendRequest();
	}
}
